package com.yrline.pay.web.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yrline.pay.proxy.PayInvoke;
import com.yrline.pay.proxy.request.AssureProjectRequest;
import com.yrline.pay.proxy.request.AutoSignRequest;
import com.yrline.pay.proxy.request.CommonRequest;
import com.yrline.pay.proxy.request.FreezeRequest;
import com.yrline.pay.proxy.request.ProjectRegRequest;
import com.yrline.pay.proxy.request.RechargeRequest;
import com.yrline.pay.proxy.request.UserParam;
import com.yrline.pay.proxy.response.AssureProjectResponse;
import com.yrline.pay.proxy.response.BankDetails;
import com.yrline.pay.proxy.response.ProjectRegResponse;
import com.yrline.pay.services.BankInfoServices;
import com.yrline.pay.services.OperatorServices;
import com.yrline.pay.services.ProjectServices;
import com.yrline.pay.services.RechargeServices;
import com.yrline.pay.services.UserServices;
import com.yrline.pay.services.vo.UserVo;
import com.yrline.pay.util.DateUtils;
import com.yrline.pay.util.IdFactory;
import com.yrline.pay.web.CommonUtil;
import com.yrline.pay.web.PayUtil;
import com.yrline.pay.web.ResponseVo;

@Controller
public class AccessController {
	@Autowired
	PayInvoke payInvoke;
	@Autowired
	PayUtil payUtil;
	@Autowired
	BankInfoServices bankInfoServices;
	@Autowired
	RechargeServices rechargeServices;
	@Autowired
	OperatorServices operatorServices;
	private Logger logger = org.slf4j.LoggerFactory
			.getLogger(AccessController.class);

	
	@Autowired
	private UserServices userServices;
	@Autowired
	private ProjectServices projectServices;
	
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index(Model model) {
		model.addAttribute("name", "Dear");
		return "index";
	}
	
	
	@RequestMapping(value = "/redirect", method = RequestMethod.GET)
	public String toUser(@RequestParam String redirect) {
		return redirect;
	}
	@RequestMapping(value = "/invokeUser", method = RequestMethod.POST)
	@ResponseBody
	public ResponseVo<CommonRequest> invokeUser(@Valid  UserParam up){
		up.setMerBillNo(CommonUtil.getMerBillNo());
		up.setMerDate(DateUtils.dateParseShortString(new Date()));
		up.setUserName("yrline"+DateUtils.getCurrentTimeNumber());
		CommonRequest request = payUtil.getDefaultRequest(up, "user.register");
		payInvoke.sign(request);
		userServices.saveUser(up);
		return new ResponseVo<CommonRequest>(request);
	}
	
	@RequestMapping(value = "/users", method = RequestMethod.GET)
	@ResponseBody
	public ResponseVo<List<UserVo>> users(@RequestParam(required=false) Integer type){
		List<UserVo> _result = userServices.selectUser(type);
		return ResponseVo.getSuccessResponse( _result);
	}

	@RequestMapping(value = "/regProject", method = RequestMethod.POST)
	@ResponseBody
	public ResponseVo<Boolean> regProject(@Valid  ProjectRegRequest up){
		up.setMerBillNo(CommonUtil.getMerBillNo());
		up.setMerDate(new Date());
		up.setProjectNo(IdFactory.randomNum(10));
		UserVo userParam =userServices.selectUserOne(up.getFinaIpsAcctNo(), up.getFinaAccType());
		up.setFinaCertNo(userParam.getIdentNo());
		up.setFinaName(userParam.getRealName());
		up.setFinaIpsAcctNo(userParam.getIpsAcctNo());
		
		CommonRequest request = payUtil.getDefaultRequest(up, "project.regProject");
		ProjectRegResponse regResponse =(ProjectRegResponse)payInvoke.invoke(request, ProjectRegResponse.class);
		if(regResponse.getStatus()==1){
			up.setIpsBillNo(regResponse.getIpsBillNo());
			projectServices.save(up);
			return ResponseVo.getSuccessResponse(true);
		}else{
			return new ResponseVo<Boolean>(false, 200, regResponse.getIpsReturnMsg());
		}
		
	}
	
	@RequestMapping(value = "/bankcards", method = RequestMethod.GET)
	@ResponseBody
	public ResponseVo<List<BankDetails>> bankcards(){
		return ResponseVo.getSuccessResponse( bankInfoServices.selectAll());
	}
	
	
	@RequestMapping(value = "/recharge", method = RequestMethod.POST)
	@ResponseBody
	public ResponseVo<CommonRequest>  recharge(@Valid  RechargeRequest up){
		up.setMerBillNo(CommonUtil.getMerBillNo());
		up.setMerDate(new Date());
		if(up.getDepositType()==2){
			up.setBankCode(null);
		}
		
		CommonRequest request = payUtil.getDefaultRequest(up, "trade.deposit");
		payInvoke.sign(request);
		rechargeServices.saveRecord(up);
		return new ResponseVo<CommonRequest>(request);
		
	}
	@RequestMapping(value = "/freeze", method = RequestMethod.POST)
	@ResponseBody
	public ResponseVo<CommonRequest>  freeze(@Valid  FreezeRequest up){
		
		if(up.getBizType()!=1){
			return new ResponseVo<CommonRequest>(false, 200, "不支持");
		}
		up.setMerBillNo(CommonUtil.getMerBillNo());
		up.setMerDate(new Date());
		up.setRegType(1);
		up.setFreezeMerType(1);
		
		CommonRequest request = payUtil.getDefaultRequest(up, "trade.freeze");
		payInvoke.sign(request);
		operatorServices.save(up);
		return new ResponseVo<CommonRequest>(request);
		
	}
	@RequestMapping(value = "/loan", method = RequestMethod.POST)
	@ResponseBody
	public ResponseVo<Boolean>  loan(@RequestParam String projectNo){
		operatorServices.loan(projectNo);
		return ResponseVo.getSuccessResponse(true);
		
	}
	@RequestMapping(value = "/autoLogin", method = RequestMethod.POST)
	@ResponseBody
	public ResponseVo<CommonRequest>  autoLogin(@Valid  AutoSignRequest up){
		up.setMerBillNo(CommonUtil.getMerBillNo());
		up.setMerDate(new Date());
		up.setValidity(365);
		if(up.getSignedType()==0){
			up.setBidMax(new BigDecimal(1000000));
			up.setBidMin(new BigDecimal(1));
			up.setCycMinVal(1);
			up.setCycMaxVal(500);
			up.setRateMin("1.0%");
			up.setRateMax("20.0%");
		}
		
		
		CommonRequest request = payUtil.getDefaultRequest(up, "user.autoSign");
		payInvoke.sign(request);
		return new ResponseVo<CommonRequest>(request);
		
		
	}
	
	@RequestMapping(value = "/assureProject", method = RequestMethod.POST)
	@ResponseBody
	public ResponseVo<Boolean> assureProject(@Valid  AssureProjectRequest up){
		up.setMerBillNo(CommonUtil.getMerBillNo());
		up.setMerDate(new Date());

		CommonRequest request = payUtil.getDefaultRequest(up, "project.assureProject");
		AssureProjectResponse regResponse =(AssureProjectResponse)payInvoke.invoke(request, AssureProjectResponse.class);
		if(regResponse.getStatus()==1){
			projectServices.assureProject(up);
			return ResponseVo.getSuccessResponse(true);
		}else{
			return new ResponseVo<Boolean>(false, 200, regResponse.getIpsReturnMsg());
		}
		
	}
	@RequestMapping(value = "/redPack", method = RequestMethod.POST)
	@ResponseBody
	public ResponseVo<Boolean> assureProject(@RequestParam String userIpsAccount,@RequestParam Integer usrType,@RequestParam BigDecimal amount){
		
		
		if(this.operatorServices.redPack(userIpsAccount, usrType, amount)){
			return ResponseVo.getSuccessResponse(true);
		}else{
			return new ResponseVo<Boolean>(false, 200, "发放失败");
		}
		
		
	}
	
}
